<?php

class StldsController extends \BaseController {

	/**
	 * Display a listing of stlds
	 *
	 * @return Response
	 */
	public function index()
	{
		$pendidikans = array('' => 'Semua pendidikan') + Pendidikan::where('is_active', '1')->orderBy('id','DSC')->lists('nama', 'id');
		$jenispekerjaans = array('' => 'Semua pekerjaan') + JenisPekerjaan::where('is_active', '1')->orderBy('id','DSC')->lists('nama', 'id');
		$agamas = array('' => 'Semua Agama') + Agama::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');

		return View::make('stlds.index')
						->with(compact('pendidikans'))
						->with(compact('lokasis'))
						->with(compact('agamas'))
						->with(compact('jenispekerjaans'));
	}

	/**
	 * Show the form for creating a new stld
	 *
	 * @return Response
	 */
	public function create()
	{
		$pendidikans = array('' => '- - Pilih Jenis Pendidikan - -') + Pendidikan::where('is_active', '1')->orderBy('id','DSC')->lists('nama', 'id');
		$jenispekerjaans = array('' => '- - Pilih Jenis Pekerjaan - -') + JenisPekerjaan::where('is_active', '1')->orderBy('id','DSC')->lists('nama', 'id');
		$lokasis = array('' => '- - Pilih Lokasi Penjamin - -') + Lokasi::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		$agamas = array('' => '- - Pilih Agama - -') + Agama::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');
		
		return View::make('stlds.create')
							->with(compact('pendidikans'))
							->with(compact('jenispekerjaans'))
							->with(compact('agamas'))
							->with(compact('lokasis'));
	}

	/**
	 * Store a newly created stld in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), Stld::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Stld::create($data);

		return Redirect::route('stlds.index');
	}

	/**
	 * Display the specified stld.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$stld = Stld::findOrFail($id);

		return View::make('stlds.show', compact('stld'));
	}

	/**
	 * Show the form for editing the specified stld.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$stld = Stld::find($id);
		$pendidikans = array('' => '- - Pilih Jenis Pendidikan - -') + Pendidikan::where('is_active', '1')->orderBy('id','DSC')->lists('nama', 'id');
		$jenispekerjaans = array('' => '- - Pilih Jenis Pekerjaan - -') + JenisPekerjaan::where('is_active', '1')->orderBy('id','DSC')->lists('nama', 'id');
		$agamas = array('' => '- - Pilih Agama - -') + Agama::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');

		return View::make('stlds.edit')
							->with(compact('stld'))
							->with(compact('pendidikans'))
							->with(compact('agamas'))
							->with(compact('jenispekerjaans'));
	}

	/**
	 * Update the specified stld in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$stld = Stld::findOrFail($id);

		$validator = Validator::make($data = Input::all(), Stld::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Stld::create($data);

		$stld = Stld::findOrFail($id);
		$stld->is_active = 0;
		$stld->save();

		return Redirect::route('stlds.index');
	}

	/**
	 * Remove the specified stld from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$stld = Stld::findOrFail($id);
		$stld->is_active = 0;
		$stld->action = 3;
		$stld->save();

		return Redirect::route('stlds.index');
	}

	public function filter()
	{
		$pendidikans = array('' => 'Semua pendidikan') + Pendidikan::where('is_active', '1')->orderBy('id','DSC')->lists('nama', 'id');
		$jenispekerjaans = array('' => 'Semua pekerjaan') + JenisPekerjaan::where('is_active', '1')->orderBy('id','DSC')->lists('nama', 'id');
		$agamas = array('' => 'Semua Agama') + Agama::where('is_active', '1')->orderBy('nama','ASC')->lists('nama', 'id');

		return View::make('stlds.filter')
						->with(compact('pendidikans'))
						->with(compact('lokasis'))
						->with(compact('agamas'))
						->with(compact('jenispekerjaans'));
	}

	public function datatableAllStlds()
	{
		$stlds = DB::table('stlds')
    					->join('jenis_pekerjaans', 'stlds.jenis_pekerjaan_id', '=', 'jenis_pekerjaans.id')
    					->join('pendidikans', 'stlds.pendidikan_id', '=', 'pendidikans.id')
    					->join('lokasis', 'stlds.lokasi_id', '=', 'lokasis.id')
    					->join('agamas', 'stlds.agama_id', '=', 'agamas.id')
    					->select(array(
    						'stlds.id',
    						'stlds.no_ktp',
    						'stlds.nama',
    						'agamas.nama AS agamas',
    						DB::raw('YEAR(CURDATE())-YEAR(stlds.tanggal_lahir) AS umur'),
    						'stlds.jenis_kelamin',
    						'stlds.status_kawin',
    						'jenis_pekerjaans.nama AS jenis_pekerjaan',
    						'pendidikans.nama AS pendidikan',
    						'lokasis.nama AS lokasi'
    						));

  	if(Input::get('fl_no_ktp') != "") {
  		$stlds = $stlds->where('stlds.no_ktp', '=', Input::get('fl_no_ktp'));	
  	}
		if(Input::get('fl_nama') != "") {
  		$stlds = $stlds->where('stlds.nama', 'LIKE', '%'.Input::get('fl_nama').'%');
  	}
  	if(Input::get('fl_agama') != "") {
  		$stlds = $stlds->where('stlds.agama_id', '=', Input::get('fl_agama'));
  	}
  	if(Input::get('fl_jk') != "") {
  		$stlds = $stlds->where('stlds.jenis_kelamin', '=', Input::get('fl_jk'));
  	}
  	if(Input::get('fl_pendidikan') != "") {
  		$stlds = $stlds->where('stlds.pendidikan_id', '=', Input::get('fl_pendidikan'));
  	}
  	if(Input::get('fl_pekerjaan') != "") {
  		$stlds = $stlds->where('stlds.jenis_pekerjaan_id', '=', Input::get('fl_pekerjaan'));
  	}
  	if(Input::get('fl_kawin') != "") {
  		$stlds = $stlds->where('stlds.status_kawin', '=', Input::get('fl_kawin'));
  	}
  	if(Input::get('fl_lokasi') != "") {
  		$stlds = $stlds->where('lokasis.nama', 'LIKE', '%'.Input::get('fl_lokasi').'%');
  	}
  	if(Input::get('fl_umur_awal') != "") {
  		$stlds = $stlds->where(DB::raw('YEAR(CURDATE())-YEAR(stlds.tanggal_lahir)'), '>=', Input::get('fl_umur_awal'));
  	}
  	if(Input::get('fl_umur_akhir') != "") {
  		$stlds = $stlds->where(DB::raw('YEAR(CURDATE())-YEAR(stlds.tanggal_lahir)'), '<=', Input::get('fl_umur_akhir'));
  	}
  	$stlds = $stlds->where('stlds.is_active', '=', '1')->orderBy('stlds.nama','ASC');

		return Datatables::of($stlds)
		->add_column('operations', '<div class="btn-group">
										<a href="{{ URL::route(\'stlds.show\', $id) }}" class="btn btn-default"><i class="fa fa-list text-success btn-tooltip" data-toggle="tooltip" data-placement="top" data-original-title="Detail"></i></a>
										<a href="{{ URL::route(\'stlds.edit\', $id) }}" class="btn btn-default"><i class="fa fa-pencil text-warning btn-tooltip" data-toggle="tooltip" data-placement="top" data-original-title="Edit"></i></a>
										<a href="{{ URL::route(\'stlds.destroy\', $id) }}" class="btn btn-default btn-delete"><i class="fa fa-times text-danger btn-tooltip" data-toggle="tooltip" data-placement="top" data-original-title="Hapus"></i></a>
									</div>')
		->make();
	}

}
